import java.io.*;

/**
 * Created By acproject@qq.com on 3/11/22 1:32 PM
 */
public class FileUtil {
    /**
     *
     * @param target 输出目标的文件路径和文件名
     * @param src 读取的文件流
     * @throws IOException
     */
    public static void write(String target, InputStream src) throws IOException {
        OutputStream os =new FileOutputStream(target);
        byte[] buf = new byte[1024];
        int len;
        while (-1 != (len=src.read(buf))) {
            os.write(buf,0,len);
        }
        os.flush();
        os.close();
    }

    /**
     *
     * @param target 输出目标的文件路径和文件名
     * @param targetSize 输出文件的总长度(是断点后的实际长度)
     * @param src 原文件的流
     * @param srcSzie 源文件的总长度
     * @param chucks 被分块的总数
     * @param chuck 当前的块的计数
     * @throws IOException
     */
    public static void writeWithBlok(String target, long targetSize, InputStream src,
                                     long srcSzie, int chucks, int chuck) throws IOException {
        RandomAccessFile randomAccessFile = new RandomAccessFile(target, "rw");
        if (chuck == chucks - 1) {
            randomAccessFile.seek(targetSize - srcSzie);
        } else {
            randomAccessFile.seek(chuck * srcSzie);
        }
        byte[] buf = new byte[1024];
        int len;
        while (-1 != (len=src.read(buf))){
            randomAccessFile.write(buf,0,len);
        }


    }
}
